package myStruts;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class uploadAction extends ActionSupport {
	File pic1;

	public File getPic1() {
		return pic1;
	}

	public void setPic1(File pic1) {
		this.pic1 = pic1;
	}

	public String getPic1ContentType() {
		return pic1ContentType;
	}

	public void setPic1ContentType(String pic1ContentType) {
		this.pic1ContentType = pic1ContentType;
	}

	public String getPic1FileName() {
		return pic1FileName;
	}

	public void setPic1FileName(String pic1FileName) {
		this.pic1FileName = pic1FileName;
	}

	String pic1ContentType;
	String pic1FileName;

	public String execute() {
		this.LOG.info("execute");
		return "input";
	}

	public String upload() throws Exception {
		this.LOG.info("upload");
		this.LOG.info("pic1:" + pic1);
		this.LOG.info("pic1ContentType:" + pic1ContentType);
		this.LOG.info("pic1FileName:" + pic1FileName);
		String saveDir = ServletActionContext.getServletContext().getRealPath(
				"upload")
				+ "\\" + pic1FileName;
		this.LOG.info("saveDir:" + saveDir);
		String source = pic1.getAbsolutePath();
		this.LOG.info("source:" + source);
		this.copyFile(source,saveDir);
		return "list";
	}
	
	public void copyFile(String source,String saveDir){
		try{
		FileChannel src=new FileInputStream(source).getChannel();
		FileChannel dst=new FileOutputStream(saveDir).getChannel();
		dst.transferFrom(src, 0, src.size());
		src.close();
		dst.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

}
